package com.cb.domain.service.Insurance.Impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.cb.common.util.ConfigUtil;
import com.cb.common.util.DateUtil;
import com.cb.common.webService.ClientService;
import com.cb.domain.model.order.BOrder;
import com.cb.domain.model.order.Flight;
import com.cb.domain.model.order.Passenger;
import com.cb.domain.service.Insurance.InsuranceSaver;

@Service("hybx")
public class Hybx implements InsuranceSaver {

	/**
	 * 太平洋养老保险
	 */
	@Override
	public String saveInsuranceByBOrder(BOrder order,Passenger passenger) {
		ClientService<webservices.hybc.ServiceSoap> clientService = new ClientService<webservices.hybc.ServiceSoap>();
		webservices.hybc.ServiceSoap service = clientService.getClient("hybx.path",webservices.hybc.ServiceSoap.class);
		
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd");
		String date=dateFormat.format(passenger.getBirthday());
		String cardType="9";
		if(passenger.getCardType().equalsIgnoreCase("NI")){
			cardType="1";
		}
		
		Set<Flight> flights= order.getFlights();
		Flight flight=null;
		for (Flight ft : flights) {
			flight=ft;
			break;
		}
		Date date2=new Date();
		try {
			 String strD=dateFormat.format(flight.getDepDay());
			 date2=dateFormat.parse(strD);
		} catch (ParseException e) {
		}
		
		String userId=ConfigUtil.getInstance().getXmlValByStr("hybx.userId", "dmsun");
		String pswd=ConfigUtil.getInstance().getXmlValByStr("hybx.pswd", "9871d");
		
		String result=service.hyTBService(passenger.getName(), "1", date, cardType, passenger.getCardNum(), order.getContactMob(), order.getContactEmail(),
				DateUtil.convertToXMLGregorianCalendar(date2), flight.getCode(), userId, pswd);
		return result;
	}

	@Override
	public String cancleInsuranceByBOrder(String orderId, Date date) {
			ClientService<webservices.hybc.ServiceSoap> clientService = new ClientService<webservices.hybc.ServiceSoap>();
			webservices.hybc.ServiceSoap service = clientService.getClient("hybx.path",webservices.hybc.ServiceSoap.class);
		
			String userId=ConfigUtil.getInstance().getXmlValByStr("hybx.userId", "dmsun");
			String pswd=ConfigUtil.getInstance().getXmlValByStr("hybx.pswd", "9871d");
			String result=service.hyCBService(orderId, com.cb.core.util.DateUtil.getDateTime("yyyyMMdd", date), userId, pswd);
			return result;
	}

}
